package com.example.cofeed.adapter;

import android.support.annotation.Nullable;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;

import com.chad.library.adapter.base.BaseQuickAdapter;
import com.chad.library.adapter.base.BaseViewHolder;
import com.example.cofeed.R;
import com.example.cofeed.api.ApiService;
import com.example.cofeed.callback.DialogCallback;
import com.example.cofeed.callback.LzyResponse;
import com.example.cofeed.entity.History;
import com.lzy.okgo.model.Response;

import java.util.List;

public class HistoryAdapter extends BaseQuickAdapter<History, BaseViewHolder> {
    public HistoryAdapter() {
        super(R.layout.adapter_history);
    }

    @Override
    protected void convert(BaseViewHolder helper, History item) {
        helper.setText(R.id.tv_title, item.modified_time);

        RecyclerView recyclerView = helper.itemView.findViewById(R.id.rv_list);
        recyclerView.setLayoutManager(new LinearLayoutManager(mContext));
        HistorySubAdapter historySubAdapter = new HistorySubAdapter();
        recyclerView.setAdapter(historySubAdapter);
        historySubAdapter.setNewData(item.histories);

        historySubAdapter.setOnItemChildClickListener((adapter, view, position) -> {
            History history = (History) adapter.getData().get(position);

            ApiService.logview_delete_article(this, String.valueOf(history.id), new DialogCallback<LzyResponse<Object>>() {
                @Override
                public void onSuccess(Response<LzyResponse<Object>> response) {
//                    if (history.histories.size() == 1){
//                        remove(position);
//                    }else {
//                        history.histories.remove(history);
//                        historySubAdapter.notifyDataSetChanged();
//                    }

                    remove(helper.getLayoutPosition());

                }
            });

        });


    }
}
